from django.http import Http404
from rest_framework.decorators import api_view
from rest_framework.request import Request
from rest_framework.response import Response

from api.serializers.product_serializer import ProductListResponseSerializer, ProductDetailResponseSerializer
from app.models import Product


@api_view(['GET'])
def get_product_list(request: Request, category_id):
    queryset = Product.objects.filter(category=category_id, is_on_shell=True)
    serializer = ProductListResponseSerializer(list(queryset), many=True)
    return Response(serializer.data)


@api_view(['GET'])
def get_product_detail(request: Request, product_id):
    try:
        product = Product.objects.get(id=product_id)
    except Product.DoesNotExist:
        raise Http404
    serializer = ProductDetailResponseSerializer(product)
    return Response(serializer.data)
